package com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.qqInterfaceCall.File_InterCall;
import com.romotc.qqrobot.webqq.qqInterfaceCall.Get_file2_InterCall;
import com.romotc.scache.RequestSpy;
/**
 * 处理302响应，，重新生成302地址对应的request并调用
 * @author Administrator
 *
 */
public class Get_file2_processor implements DateProcessor {

	@Override
	public JSONObject processResponse(QQLogedin loginQQ,
				RequestSpy rqSpy, JSONObject reqDataObj) throws Exception{
		
		if(rqSpy == null || rqSpy.httprc != 302)		//只处理302跳转
			return reqDataObj;
		
		//跳转的地址
		String forwardUrl = rqSpy.getHeader("location");
		if(StringUtils.isNotBlank(forwardUrl))
		{
			//中文乱码问题http://file1.web.qq.com/v2/1586030808/2623237499/29523/1075/45338/0/0/1/f/22992/??????.jpg?psessionid=8368046764001d636f6e6e7365727665725f77656271714031302e3133342e362e31333800002bdf000013a9036e0400d8e8885e6d0000000a40344d4b416a696f56446d0000002866ed0f100c003fda6408cd40962edb757f807232b73dc27b6c26ceee901a427d101d8cc6ffdf5a85
			int idx = forwardUrl.lastIndexOf("/");
			int idx2 = forwardUrl.indexOf("?");
			forwardUrl = forwardUrl.substring(0, idx + 1) 
							+ java.net.URLEncoder.encode(reqDataObj
															.getString(Get_file2_InterCall.Key_Must_Input_FileName)
															,"utf-8")//这里转码一定要用utf-8格式转
							+ forwardUrl.substring(idx2);
			
			//重新发起请求 并使用文件保存processor保存文件
			reqDataObj.put(File_InterCall.Key_Must_Input_FileUrl, forwardUrl);
			File_InterCall fileCall = File_InterCall.getInstance();
			reqDataObj = fileCall.call(loginQQ, reqDataObj, SaveFile_Processor.getInstance());
		}

		return reqDataObj;
	}

	private static Get_file2_processor instance;
	public static synchronized Get_file2_processor getInstance() {
		if(instance == null)
			instance = new Get_file2_processor();
		return instance;
	}
	
	public static void main(String[] args) throws Exception{
		String string = "";//%E8%B7%B3%E8%BD%AC%E7%9A%84%E5%9C%B0%E5%9D%80d
		String string2 = java.net.URLEncoder.encode("跳转的地址d.jpg","utf-8");
		System.out.println(string2);
//		System.out.println(java.net.URLEncoder.encode(s, enc));
	}

}
